package com.zhang;

import java.util.ArrayList;
import java.util.List;

public class StrategyMode {
    public static void main(String[] args) {
        List<Fruit> fruits = new ArrayList<>();
        fruits.add(new Apple(2, 8, 1));
        fruits.add(new Mongo(2, 20, 1));
        fruits.add(new Strawberry(15, 8, 2));

        double total = 0;

        for (Fruit fruit : fruits) {
            StrategyCaculate strategyCaculate = StrategyFactory.create(fruit);
            double result = strategyCaculate.calculatePrice(fruit);
            total += result;
        }

        System.out.println("总价为：  " + total);

    }
}

class Fruit {
    protected int num;
    protected double price;
    protected int type;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public Fruit(int num, double price, int type) {
        this.num = num;
        this.price = price;
        this.type = type;
    }
}

class Apple extends Fruit {
    public Apple(int num, double price, int type) {
        super(num, price, type);
    }
}

class Strawberry extends Fruit {
    public Strawberry(int num, double price, int type) {
        super(num, price, type);
    }
}

class Mongo extends Fruit {
    public Mongo(int num, double price, int type) {
        super(num, price, type);
    }
}

interface StrategyCaculate {
    double calculatePrice(Fruit fruit);
}

class StrategyFactory {
    public static StrategyCaculate create(Fruit fruit) {
        if (fruit.type == 1) {
            return new DefaultStrategy();
        } else if (fruit.type == 2) {
            return new DiscountStrategy();
        } else {
            return new FullDecrement();
        }
    }
}

class DefaultStrategy implements StrategyCaculate {
    @Override
    public double calculatePrice(Fruit fruit) {
        return fruit.getPrice() * fruit.getNum();
    }
}

class DiscountStrategy implements StrategyCaculate {

    @Override
    public double calculatePrice(Fruit fruit) {
        return fruit.getNum() * fruit.getPrice() * 0.8;
    }
}

class FullDecrement implements StrategyCaculate {
    @Override
    public double calculatePrice(Fruit fruit) {
        double originalPrice = fruit.getPrice() * fruit.getNum();
        double muti = Math.floor(originalPrice / 100);
        return originalPrice - muti * 10;
    }
}

